#include <stdio.h>
#include <stdlib.h>

/*int main()
{
	int ret;
	FILE* pf;
	//打开文件
	pf = fopen("1.txt", "r+");
	if(pf == NULL)
	{
		perror("pf");
		return -1;
	}
	//业务操作
	fputc('a',pf);
	ret = fgetc(pf);
	if(ret == -1)
	{
		printf("获取失败\n");
		fclose(pf);
		return -1;
	}
	else
	{
		printf("%c\n",ret);
	}

	//关闭文件
	fclose(pf);
	return 0;
}*/


/*int main()
{
	int ret;
	FILE* pf;
	pf = fopen("1.txt","r");
	if(pf == NULL)
	{
		perror("pf open fait");
		return -1;
	}
	//打开文件
	//操作
	ret = fgetc(pf);
	if(ret == -1)
	{
		perror("get fait");
		return -1;
	}else
	{
		printf("%c\n",ret);
	}
	//关闭文件
	fclose(pf);
	return 0; 
}*/



/*int main()
{	
	FILE* pf;
	//打开文件
	pf = fopen("1.txt","r+");
	if( pf == NULL)
	{
		 perror("pf open fait");
		 return -1;
	}
	//业务处理
	fputs("xianghaoshui\n",pf);
	fputs("jiayou\n",pf);
	//关闭文件
	fclose(pf);
	return 0;
}*/


/*int main()
{
	char* ret;
	char* arr;
	FILE* pf;
   //打开文件
   pf = fopen("11.txt","r+");
   if( pf == NULL)
   {
		perror("pf open fait");
		return -1;
   }
   //业务处理
   ret = fgets(arr, 5, pf);
   if(ret == NULL)
   {
	   perror("fgets");
	   fclose(pf);
	   return 0;
   }else
   {
	   printf("%s\n", arr);
   }
   //关闭文件
   fclose(pf);
   return 0;
}*/


/*typedef struct stu
{
	char name[20];
	int age;
	char sex[5];
	char id[15];
}stu;

int main(int argc , char* argv[])
{
	int ret;
	stu s = {"xiaoyu", 12, "男", "202200310051"};
	FILE* pf;
	//打开文件
	pf =fopen("2.txt","w+");
	if(pf == NULL)
	{
		perror("pf");
		return 0;
	}
	//业务操作
	fwrite(&s, sizeof(s), 1, pf );
	rewind(pf);
	ret = fread(&s, sizeof(s), 1, pf);
	if(ret < 0)
	{
		perror("fread");
		return 0;
		goto end;
	}else
	{
		printf("名字:%s\n", s.name);
		printf("年龄:%d\n", s.age);
		printf("性别:%s\n", s.sex);
		printf("学号:%s\n", s.id);
	}
	//关闭文件

end:
	fclose(pf);
	printf("文件关闭成功\n");
	return 0;
}*/


//fseek练习
int main(int argc, char* argv[])
{
	FILE* pf;
	//打开文件
	pf = fopen("11.txt", "r+");
	if(pf == NULL)
	{
		perror("pf");
		return 0;
	}
	//业务处理
	fputs("asdfghjkl", pf);
	fseek(pf, -9, SEEK_END);
	fputs("abcdefg", pf);

	//关闭文件
	fclose(pf);

	return 0;
}










































